import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

public class Solution878 {
    public int nthMagicalNumber(int n, int a, int b) {
        long u=gcd(a,b);
        long t=(long)a*b/u;
        TreeSet<Long> treeSet=new TreeSet<>();
        for (int i=1;i*a<=t;i++){
            treeSet.add((long)i*a);
        }
        for (int i=1;i*b<=t;i++){
            treeSet.add((long)i*b);
        }
        n--;
        int v=n%treeSet.size();
        long p=n/treeSet.size()*t;
        List<Long> list=new ArrayList<>(treeSet);
        p=(p+list.get(v))%1000000007;
        return (int) p;
    }

    private long gcd(int a, int b) {
        if (a<b){
            int t=a;
            a=b;
            b=t;
        }
        return a%b==0?b:gcd(b,a%b);
    }

    public static void main(String[] args) {
        System.out.println(new Solution878().nthMagicalNumber(4,2,3));
    }
}
